# setup.exit.poll.R
#
# Merge Atkinson, Enos, Hill candidate face scores
# to exit poll respondents.

cat('\nSetting up exit poll data ...\n')

library(foreign)

# ---------------------------------------------------
# Get Data
#
#   The following exit polls are all available for
#   online download from the Roper Center 
#   (www.ropercenter.uconn.edu):
#       2006 National Election Pool:    usmi2006-natelec.por
#       2004 National Election Pool:    usmi2004-natelec.por
#       2002 Voter News Service:        vns2002.por        
#       2000 Voter News Service:        usvns2000-natelec.por
#       1998 Voter News Service:        vns1998.dat        
#       1996 Voter News Service:        vns1996.dat        
#       1994 Voter News Service:        vns1994.dat        
#       1992 Voter Research and Surveys:vrs1992.dat        
# ---------------------------------------------------

e06 <- read.spss("exitpolls/usmi2006-natelec.por")
exit04 <- read.spss("exitpolls/usmi2004-natelec.por")
e02<- read.spss("exitpolls/vns2002.por")
e00<- read.spss("exitpolls/usvns2000-natelec.por")
e98<- read.table("exitpolls/vns1998.dat",sep="*",colClasses="character")
e96<- read.table("exitpolls/vns1996.dat",sep="*",colClasses="character")
e94<- read.table("exitpolls/vns1994.dat",sep="*",colClasses="character")
e92<- read.table("exitpolls/vrs1992.dat",sep="*",colClasses="character")

exit.statecodes <- read.csv("exitpolls/exitpoll_statecodes.csv",as.is=T,header=T)

aeh.house <- read.csv("house.csv",as.is=T,header=T)
#Open seats only; VT winner was an independent (Sanders); TX redistricted for 2004.
aeh.house <- aeh.house[aeh.house$open.seat==0 & aeh.house$stcd!="VT1" & substr(aeh.house$stcd,1,2)!="TX",]

aeh.senate <- read.csv("senate.csv",as.is=T,header=T)
aeh.senate <- aeh.senate[substr(aeh.senate$office,7,9)=="sen" & aeh.senate$drop_contest==0,]

tenure.house <- read.csv("exitpolls/TenureHouse.csv",as.is=T,header=T)
tenure.senate <- read.csv("exitpolls/TenureSenate.csv",as.is=T,header=T)

###############################################################################
#Merge incumbent age and tenure variables to the House and Senate data frames
###############################################################################

aeh.house$IncFirstYrInOffice <- tenure.house$IncumbentFirstHouseYear[match(aeh.house$stcd,tenure.house$stcd)]
aeh.house$IncBirthYr <- tenure.house$IncumbentBirthYear[match(aeh.house$stcd,tenure.house$stcd)]
aeh.senate$IncFirstYrInOffice <- tenure.senate$FirstYrInOffice[match(aeh.senate$candidate,tenure.senate$Senator)]
aeh.senate$IncBirthYr <- tenure.senate$BirthYear[match(aeh.senate$candidate,tenure.senate$Senator)]

###################################################
# remove open seat senate records
###################################################

senate.dem <- aeh.senate[aeh.senate$party=="dem",]
senate.rep <- aeh.senate[aeh.senate$party=="rep",]
dem.incum <- ifelse(senate.dem$incumbent==1,1,0)
rep.incum <- ifelse(senate.rep$incumbent==1,1,0)
senate.rep_dem.incumbent <- dem.incum[match(senate.rep$contest,senate.dem$contest)]

unique.contest <- sort(unique(aeh.senate$contest))
unique.contest.open.seat <- ifelse(dem.incum[match(unique.contest,senate.dem$contest)]==0 & rep.incum[match(unique.contest,senate.rep$contest)]==0,1,0)
open.seat <- unique.contest.open.seat[match(aeh.senate$contest,unique.contest)]

# drop open seat races
aeh.senate <- aeh.senate[open.seat==0,]

###############################################################
# North Carolina 1992 is coded LeanRep with a Dem incumbent.
###############################################################

aeh.senate <- aeh.senate[aeh.senate$office!="1992NCsen",]

###############################################################################################
# SENATE: construct variables
###############################################################################################

senate.dem <- aeh.senate[aeh.senate$party=="dem",]
senate.rep <- aeh.senate[aeh.senate$party=="rep",]
senate.id <- paste(aeh.senate$year,aeh.senate$state,sep="")
senate.dem.id <- paste(senate.dem$year,senate.dem$state,sep="")
senate.rep.id <- paste(senate.rep$year,senate.rep$state,sep="")
senate.dem.receipts <- senate.dem$Receipts
senate.rep.receipts <- senate.rep$Receipts
senate.dem.expend <- senate.dem$Disbursements
senate.rep.expend <- senate.rep$Disbursements

dem.incum <- ifelse(senate.dem$incumbent==1,1,0)
rep.incum <- ifelse(senate.rep$incumbent==1,1,0)

dem.pres.margin <- ifelse(aeh.senate$year==1990,aeh.senate$dem88/(aeh.senate$dem88+aeh.senate$rep88),
    ifelse(aeh.senate$year==1992 | aeh.senate$year==1994,aeh.senate$dem92/(aeh.senate$dem92+aeh.senate$rep92),
    ifelse(aeh.senate$year==1996 | aeh.senate$year==1998,aeh.senate$dem96/(aeh.senate$dem96+aeh.senate$rep96),
    ifelse(aeh.senate$year==2000 | aeh.senate$year==2002,aeh.senate$dem00/(aeh.senate$dem00+aeh.senate$rep00),
    ifelse(aeh.senate$year==2004 | aeh.senate$year==2006,aeh.senate$dem04/(aeh.senate$dem04+aeh.senate$rep04),NA)))))

rep.pres.margin <- ifelse(aeh.senate$year==1990,aeh.senate$rep88/(aeh.senate$dem88+aeh.senate$rep88),
    ifelse(aeh.senate$year==1992 | aeh.senate$year==1994,aeh.senate$rep92/(aeh.senate$dem92+aeh.senate$rep92),
    ifelse(aeh.senate$year==1996 | aeh.senate$year==1998,aeh.senate$rep96/(aeh.senate$dem96+aeh.senate$rep96),
    ifelse(aeh.senate$year==2000 | aeh.senate$year==2002,aeh.senate$rep00/(aeh.senate$dem00+aeh.senate$rep00),
    ifelse(aeh.senate$year==2004 | aeh.senate$year==2006,aeh.senate$rep04/(aeh.senate$dem04+aeh.senate$rep04),NA)))))

#############################################################
# prepare senate incumbent tenure and age vars
#############################################################

sen.inc.firstyr <- aeh.senate$IncFirstYrInOffice[aeh.senate$incumbent==1]
sen.inc.birthyr <- aeh.senate$IncBirthYr[aeh.senate$incumbent==1]

#############################################################
# adjust sen challenger funds based on state pop and year
#############################################################

sen.chal.receipts <- aeh.senate$Receipts[aeh.senate$incumbent==0]
sen.chal.expend <- aeh.senate$Disbursements[aeh.senate$incumbent==0]
sen.chal.contests <- aeh.senate$contest[aeh.senate$incumbent==0]
sen.chal.receipts <- sen.chal.receipts[match(aeh.senate$contest,sen.chal.contests)] # entered in both incumbent's and challenger's rows
sen.chal.expend <- sen.chal.expend[match(aeh.senate$contest,sen.chal.contests)] # entered in both incumbent's and challenger's rows

sen.chal.expend <- ifelse(is.na(sen.chal.expend)==1 | sen.chal.expend<5000,5000,sen.chal.expend)
sen.chal.expend <- ifelse(is.na(sen.chal.expend)==1 | sen.chal.expend<5000,5000,sen.chal.expend)
sen.chal.receipts <- sen.chal.receipts/100000 # in $100,000 units
sen.chal.expend <- sen.chal.expend/100000 # in $100,000 units

sen.statepop <- ifelse(aeh.senate$year==1990,aeh.senate$pop1990,
    ifelse(aeh.senate$year==1992,aeh.senate$pop1992,
    ifelse(aeh.senate$year==1994,aeh.senate$pop1994,
    ifelse(aeh.senate$year==1996,aeh.senate$pop1996,
    ifelse(aeh.senate$year==1998,aeh.senate$pop1998,
    ifelse(aeh.senate$year==2000,aeh.senate$pop2000,
    ifelse(aeh.senate$year==2002,aeh.senate$pop2002,
    ifelse(aeh.senate$year==2004,aeh.senate$pop2004,
    ifelse(aeh.senate$year==2006,aeh.senate$pop2006,NA)))))))))
sen.statepop <- sen.statepop/1000000 # statepop in units of 1,000,000 people

##########################################################################################
# construct exit poll variables
##########################################################################################

#####################################################
# add contest id variable to each exit poll record
#####################################################

hold.state <- exit.statecodes[match(e06$STANUM,exit.statecodes[,3]),1]
e06.id <- paste(2006,hold.state,sep="")
hold.state <- exit.statecodes[match(e02$STATE,exit.statecodes[,4]),1]
e02.id <- paste(2002,hold.state,sep="")
hold.state <- exit.statecodes[match(e00$STATEID,exit.statecodes[,2]),1]
e00.id <- paste(2000,hold.state,sep="")
hold.state <- exit.statecodes[match(as.numeric(substr(e98[,1],9,10)),exit.statecodes[,2]),1]
e98.id <- paste(1998,hold.state,sep="")
hold.state <- exit.statecodes[match(as.numeric(substr(e96[,1],9,10)),exit.statecodes[,2]),1]
e96.id <- paste(1996,hold.state,sep="")
hold.state <- exit.statecodes[match(as.numeric(substr(e94[,1],9,10)),exit.statecodes[,2]),1]
e94.id <- paste(1994,hold.state,sep="")
hold.state <- exit.statecodes[match(as.numeric(substr(e92[,1],9,10)),exit.statecodes[,2]),1]
e92.id <- paste(1992,hold.state,sep="")

#################################
# From exit poll data, get respondent Senate vote (party) and respondent PID
#################################

e06.sendem <- ifelse(as.numeric(e06$SEN)==2,1,ifelse(as.numeric(e06$SEN)==3,0,NA))
e06.senrep <- ifelse(as.numeric(e06$SEN)==3,1,ifelse(as.numeric(e06$SEN)==2,0,NA))
e06.party <- ifelse(as.numeric(e06$PARTY)==1,1,ifelse(as.numeric(e06$PARTY)==2,2,3)) # 1=dem, 2=rep

e02.sendem <- ifelse(e02$SEN==1,1,ifelse(e02$SEN==2,0,NA))
e02.senrep <- ifelse(e02$SEN==2,1,ifelse(e02$SEN==1,0,NA))
e02.party <- ifelse(as.numeric(e02$PARTY)==1,1,ifelse(as.numeric(e02$PARTY)==2,2,3)) # 1=dem, 2=rep

e00.sendem <- ifelse(e00$SEN==1,1,ifelse(e00$SEN==2,0,NA))
e00.senrep <- ifelse(e00$SEN==2,1,ifelse(e00$SEN==1,0,NA))
e00.party <- ifelse(as.numeric(e00$PARTYID)==1,1,ifelse(as.numeric(e00$PARTYID)==2,2,3)) # 1=dem, 2=rep
e00.votedhouse <- as.numeric(!is.na(e00$HOU))

e98.sendem <- ifelse(as.numeric(substr(e98[,1],23,23))==1,1,ifelse(as.numeric(substr(e98[,1],23,23))==2,0,NA))
e98.senrep <- ifelse(as.numeric(substr(e98[,1],23,23))==2,1,ifelse(as.numeric(substr(e98[,1],23,23))==1,0,NA))
e98.party <- ifelse(as.numeric(substr(e98[,1],70,70))==1,1,ifelse(as.numeric(substr(e98[,1],70,70))==2,2,3)) # 1=dem, 2=rep

e96.sendem <- ifelse(as.numeric(substr(e96[,1],23,23))==1,1,ifelse(as.numeric(substr(e96[,1],23,23))==2,0,NA))
e96.senrep <- ifelse(as.numeric(substr(e96[,1],23,23))==2,1,ifelse(as.numeric(substr(e96[,1],23,23))==1,0,NA))
e96.party <- ifelse(as.numeric(substr(e96[,1],70,70))==1,1,ifelse(as.numeric(substr(e96[,1],70,70))==2,2,3)) # 1=dem, 2=rep

e94.sendem <- ifelse(as.numeric(substr(e94[,1],23,23))==1,1,ifelse(as.numeric(substr(e94[,1],23,23))==2,0,NA))
e94.senrep <- ifelse(as.numeric(substr(e94[,1],23,23))==2,1,ifelse(as.numeric(substr(e94[,1],23,23))==1,0,NA))
e94.party <- ifelse(as.numeric(substr(e94[,1],70,70))==1,1,ifelse(as.numeric(substr(e94[,1],70,70))==2,2,3)) # 1=dem, 2=rep

e92.sendem <- ifelse(as.numeric(substr(e92[,1],23,23))==1,1,ifelse(as.numeric(substr(e92[,1],23,23))==2,0,NA))
e92.senrep <- ifelse(as.numeric(substr(e92[,1],23,23))==2,1,ifelse(as.numeric(substr(e92[,1],23,23))==1,0,NA))
e92.party <- ifelse(as.numeric(substr(e92[,1],70,70))==1,1,ifelse(as.numeric(substr(e92[,1],70,70))==2,2,3)) # 1=dem, 2=rep

########################################
# From AEH survey, get Senate candidate face scores
########################################

e06.demface <- senate.dem$comp[match(e06.id,senate.dem.id)]
e06.repface <- senate.rep$comp[match(e06.id,senate.rep.id)]
e02.demface <- senate.dem$comp[match(e02.id,senate.dem.id)]
e02.repface <- senate.rep$comp[match(e02.id,senate.rep.id)]
e00.demface <- senate.dem$comp[match(e00.id,senate.dem.id)]
e00.repface <- senate.rep$comp[match(e00.id,senate.rep.id)]
e98.demface <- senate.dem$comp[match(e98.id,senate.dem.id)]
e98.repface <- senate.rep$comp[match(e98.id,senate.rep.id)]
e96.demface <- senate.dem$comp[match(e96.id,senate.dem.id)]
e96.repface <- senate.rep$comp[match(e96.id,senate.rep.id)]
e94.demface <- senate.dem$comp[match(e94.id,senate.dem.id)]
e94.repface <- senate.rep$comp[match(e94.id,senate.rep.id)]
e92.demface <- senate.dem$comp[match(e92.id,senate.dem.id)]
e92.repface <- senate.rep$comp[match(e92.id,senate.rep.id)]

#########################################
# From AEH context data, get Senate candidate incumbency status
#########################################

e06.dem.incum <- ifelse(dem.incum[match(e06.id,senate.dem.id)]==1,1,0)
e06.rep.incum <- ifelse(rep.incum[match(e06.id,senate.rep.id)]==1,1,0)

e02.dem.incum <- ifelse(dem.incum[match(e02.id,senate.dem.id)]==1,1,0)
e02.rep.incum <- ifelse(rep.incum[match(e02.id,senate.rep.id)]==1,1,0)

e00.dem.incum <- ifelse(dem.incum[match(e00.id,senate.dem.id)]==1,1,0)
e00.rep.incum <- ifelse(rep.incum[match(e00.id,senate.rep.id)]==1,1,0)

e98.dem.incum <- ifelse(dem.incum[match(e98.id,senate.dem.id)]==1,1,0)
e98.rep.incum <- ifelse(rep.incum[match(e98.id,senate.rep.id)]==1,1,0)

e96.dem.incum <- ifelse(dem.incum[match(e96.id,senate.dem.id)]==1,1,0)
e96.rep.incum <- ifelse(rep.incum[match(e96.id,senate.rep.id)]==1,1,0)

e94.dem.incum <- ifelse(dem.incum[match(e94.id,senate.dem.id)]==1,1,0)
e94.rep.incum <- ifelse(rep.incum[match(e94.id,senate.rep.id)]==1,1,0)

e92.dem.incum <- ifelse(dem.incum[match(e92.id,senate.dem.id)]==1,1,0)
e92.rep.incum <- ifelse(rep.incum[match(e92.id,senate.rep.id)]==1,1,0)

e06.open.seat <- ifelse(e06.dem.incum==0 & e06.rep.incum==0,1,0)
e02.open.seat <- ifelse(e02.dem.incum==0 & e02.rep.incum==0,1,0)
e00.open.seat <- ifelse(e00.dem.incum==0 & e00.rep.incum==0,1,0)
e98.open.seat <- ifelse(e98.dem.incum==0 & e98.rep.incum==0,1,0)
e96.open.seat <- ifelse(e96.dem.incum==0 & e96.rep.incum==0,1,0)
e94.open.seat <- ifelse(e94.dem.incum==0 & e94.rep.incum==0,1,0)
e92.open.seat <- ifelse(e92.dem.incum==0 & e92.rep.incum==0,1,0)

########################
# pres.margin
########################

e92.dem.pres.margin <- dem.pres.margin[match(e92.id,senate.dem.id)]
e94.dem.pres.margin <- dem.pres.margin[match(e94.id,senate.dem.id)]
e96.dem.pres.margin <- dem.pres.margin[match(e96.id,senate.dem.id)]
e98.dem.pres.margin <- dem.pres.margin[match(e98.id,senate.dem.id)]
e00.dem.pres.margin <- dem.pres.margin[match(e00.id,senate.dem.id)]
e02.dem.pres.margin <- dem.pres.margin[match(e02.id,senate.dem.id)]
e06.dem.pres.margin <- dem.pres.margin[match(e06.id,senate.dem.id)]

e92.rep.pres.margin <- rep.pres.margin[match(e92.id,senate.dem.id)]
e94.rep.pres.margin <- rep.pres.margin[match(e94.id,senate.dem.id)]
e96.rep.pres.margin <- rep.pres.margin[match(e96.id,senate.dem.id)]
e98.rep.pres.margin <- rep.pres.margin[match(e98.id,senate.dem.id)]
e00.rep.pres.margin <- rep.pres.margin[match(e00.id,senate.dem.id)]
e02.rep.pres.margin <- rep.pres.margin[match(e02.id,senate.dem.id)]
e06.rep.pres.margin <- rep.pres.margin[match(e06.id,senate.dem.id)]

e92.inc.yrsinoffice <- 1992-sen.inc.firstyr[match(e92.id,senate.dem.id)]
e94.inc.yrsinoffice <- 1994-sen.inc.firstyr[match(e94.id,senate.dem.id)]
e96.inc.yrsinoffice <- 1996-sen.inc.firstyr[match(e96.id,senate.dem.id)]
e98.inc.yrsinoffice <- 1998-sen.inc.firstyr[match(e98.id,senate.dem.id)]
e00.inc.yrsinoffice <- 2000-sen.inc.firstyr[match(e00.id,senate.dem.id)]
e02.inc.yrsinoffice <- 2002-sen.inc.firstyr[match(e02.id,senate.dem.id)]
e06.inc.yrsinoffice <- 2006-sen.inc.firstyr[match(e06.id,senate.dem.id)]

e92.inc.age <- 1992-sen.inc.birthyr[match(e92.id,senate.dem.id)]
e94.inc.age <- 1994-sen.inc.birthyr[match(e94.id,senate.dem.id)]
e96.inc.age <- 1996-sen.inc.birthyr[match(e96.id,senate.dem.id)]
e98.inc.age <- 1998-sen.inc.birthyr[match(e98.id,senate.dem.id)]
e00.inc.age <- 2000-sen.inc.birthyr[match(e00.id,senate.dem.id)]
e02.inc.age <- 2002-sen.inc.birthyr[match(e02.id,senate.dem.id)]
e06.inc.age <- 2006-sen.inc.birthyr[match(e06.id,senate.dem.id)]

e92.chal.receipts <- sen.chal.receipts[match(e92.id,senate.dem.id)]
e94.chal.receipts <- sen.chal.receipts[match(e94.id,senate.dem.id)]
e96.chal.receipts <- sen.chal.receipts[match(e96.id,senate.dem.id)]
e98.chal.receipts <- sen.chal.receipts[match(e98.id,senate.dem.id)]
e00.chal.receipts <- sen.chal.receipts[match(e00.id,senate.dem.id)]
e02.chal.receipts <- sen.chal.receipts[match(e02.id,senate.dem.id)]
e06.chal.receipts <- sen.chal.receipts[match(e06.id,senate.dem.id)]

e92.chal.expend <- sen.chal.expend[match(e92.id,senate.dem.id)]
e94.chal.expend <- sen.chal.expend[match(e94.id,senate.dem.id)]
e96.chal.expend <- sen.chal.expend[match(e96.id,senate.dem.id)]
e98.chal.expend <- sen.chal.expend[match(e98.id,senate.dem.id)]
e00.chal.expend <- sen.chal.expend[match(e00.id,senate.dem.id)]
e02.chal.expend <- sen.chal.expend[match(e02.id,senate.dem.id)]
e06.chal.expend <- sen.chal.expend[match(e06.id,senate.dem.id)]

e92.sen.statepop <- sen.statepop[match(e92.id,senate.dem.id)]
e94.sen.statepop <- sen.statepop[match(e94.id,senate.dem.id)]
e96.sen.statepop <- sen.statepop[match(e96.id,senate.dem.id)]
e98.sen.statepop <- sen.statepop[match(e98.id,senate.dem.id)]
e00.sen.statepop <- sen.statepop[match(e00.id,senate.dem.id)]
e02.sen.statepop <- sen.statepop[match(e02.id,senate.dem.id)]
e06.sen.statepop <- sen.statepop[match(e06.id,senate.dem.id)]

##################################################
# Construct vote choice and candidate face in terms of incumbent vs. challenger
##################################################

e06.vote.chal <- ifelse(e06.dem.incum==1 & e06.rep.incum==0,e06.senrep,ifelse(e06.dem.incum==0 & e06.rep.incum==1,e06.sendem,NA))
e06.face.chal <- ifelse(e06.dem.incum==1 & e06.rep.incum==0,e06.repface,ifelse(e06.dem.incum==0 & e06.rep.incum==1,e06.demface,NA))
e06.face.inc <- ifelse(e06.dem.incum==1 & e06.rep.incum==0,e06.demface,ifelse(e06.dem.incum==0 & e06.rep.incum==1,e06.repface,NA))

e02.vote.chal <- ifelse(e02.dem.incum==1 & e02.rep.incum==0,e02.senrep,ifelse(e02.dem.incum==0 & e02.rep.incum==1,e02.sendem,NA))
e02.face.chal <- ifelse(e02.dem.incum==1 & e02.rep.incum==0,e02.repface,ifelse(e02.dem.incum==0 & e02.rep.incum==1,e02.demface,NA))
e02.face.inc <- ifelse(e02.dem.incum==1 & e02.rep.incum==0,e02.demface,ifelse(e02.dem.incum==0 & e02.rep.incum==1,e02.repface,NA))

e00.vote.chal <- ifelse(e00.dem.incum==1 & e00.rep.incum==0,e00.senrep,ifelse(e00.dem.incum==0 & e00.rep.incum==1,e00.sendem,NA))
e00.face.chal <- ifelse(e00.dem.incum==1 & e00.rep.incum==0,e00.repface,ifelse(e00.dem.incum==0 & e00.rep.incum==1,e00.demface,NA))
e00.face.inc <- ifelse(e00.dem.incum==1 & e00.rep.incum==0,e00.demface,ifelse(e00.dem.incum==0 & e00.rep.incum==1,e00.repface,NA))

e98.vote.chal <- ifelse(e98.dem.incum==1 & e98.rep.incum==0,e98.senrep,ifelse(e98.dem.incum==0 & e98.rep.incum==1,e98.sendem,NA))
e98.face.chal <- ifelse(e98.dem.incum==1 & e98.rep.incum==0,e98.repface,ifelse(e98.dem.incum==0 & e98.rep.incum==1,e98.demface,NA))
e98.face.inc <- ifelse(e98.dem.incum==1 & e98.rep.incum==0,e98.demface,ifelse(e98.dem.incum==0 & e98.rep.incum==1,e98.repface,NA))

e96.vote.chal <- ifelse(e96.dem.incum==1 & e96.rep.incum==0,e96.senrep,ifelse(e96.dem.incum==0 & e96.rep.incum==1,e96.sendem,NA))
e96.face.chal <- ifelse(e96.dem.incum==1 & e96.rep.incum==0,e96.repface,ifelse(e96.dem.incum==0 & e96.rep.incum==1,e96.demface,NA))
e96.face.inc <- ifelse(e96.dem.incum==1 & e96.rep.incum==0,e96.demface,ifelse(e96.dem.incum==0 & e96.rep.incum==1,e96.repface,NA))

e94.vote.chal <- ifelse(e94.dem.incum==1 & e94.rep.incum==0,e94.senrep,ifelse(e94.dem.incum==0 & e94.rep.incum==1,e94.sendem,NA))
e94.face.chal <- ifelse(e94.dem.incum==1 & e94.rep.incum==0,e94.repface,ifelse(e94.dem.incum==0 & e94.rep.incum==1,e94.demface,NA))
e94.face.inc <- ifelse(e94.dem.incum==1 & e94.rep.incum==0,e94.demface,ifelse(e94.dem.incum==0 & e94.rep.incum==1,e94.repface,NA))

e92.vote.chal <- ifelse(e92.dem.incum==1 & e92.rep.incum==0,e92.senrep,ifelse(e92.dem.incum==0 & e92.rep.incum==1,e92.sendem,NA))
e92.face.chal <- ifelse(e92.dem.incum==1 & e92.rep.incum==0,e92.repface,ifelse(e92.dem.incum==0 & e92.rep.incum==1,e92.demface,NA))
e92.face.inc <- ifelse(e92.dem.incum==1 & e92.rep.incum==0,e92.demface,ifelse(e92.dem.incum==0 & e92.rep.incum==1,e92.repface,NA))

e92.inc.pres.margin <- ifelse(e92.dem.incum==1 & e92.rep.incum==0,e92.dem.pres.margin,ifelse(e92.dem.incum==0 & e92.rep.incum==1,e92.rep.pres.margin,NA))
e94.inc.pres.margin <- ifelse(e94.dem.incum==1 & e94.rep.incum==0,e94.dem.pres.margin,ifelse(e94.dem.incum==0 & e94.rep.incum==1,e94.rep.pres.margin,NA))
e96.inc.pres.margin <- ifelse(e96.dem.incum==1 & e96.rep.incum==0,e96.dem.pres.margin,ifelse(e96.dem.incum==0 & e96.rep.incum==1,e96.rep.pres.margin,NA))
e98.inc.pres.margin <- ifelse(e98.dem.incum==1 & e98.rep.incum==0,e98.dem.pres.margin,ifelse(e98.dem.incum==0 & e98.rep.incum==1,e98.rep.pres.margin,NA))
e00.inc.pres.margin <- ifelse(e00.dem.incum==1 & e00.rep.incum==0,e00.dem.pres.margin,ifelse(e00.dem.incum==0 & e00.rep.incum==1,e00.rep.pres.margin,NA))
e02.inc.pres.margin <- ifelse(e02.dem.incum==1 & e02.rep.incum==0,e02.dem.pres.margin,ifelse(e02.dem.incum==0 & e02.rep.incum==1,e02.rep.pres.margin,NA))
e06.inc.pres.margin <- ifelse(e06.dem.incum==1 & e06.rep.incum==0,e06.dem.pres.margin,ifelse(e06.dem.incum==0 & e06.rep.incum==1,e06.rep.pres.margin,NA))

################################################
# construct respondent PID in terms of challenger vs. incumbent
################################################

e06.shares.chal.party <- ifelse(e06.dem.incum==1 & e06.rep.incum==0 & e06.party==2,1,ifelse(e06.dem.incum==0 & e06.rep.incum==1 & e06.party==1,1,ifelse(e06.dem.incum==1 & e06.rep.incum==0 & e06.party==1,0,ifelse(e06.dem.incum==0 & e06.rep.incum==1 & e06.party==2,0,ifelse(e06.party==3,0,NA)))))
e06.shares.inc.party <- ifelse(e06.dem.incum==1 & e06.rep.incum==0 & e06.party==1,1,ifelse(e06.dem.incum==0 & e06.rep.incum==1 & e06.party==2,1,ifelse(e06.dem.incum==1 & e06.rep.incum==0 & e06.party==2,0,ifelse(e06.dem.incum==0 & e06.rep.incum==1 & e06.party==1,0,ifelse(e06.party==3,0,NA)))))

e02.shares.chal.party <- ifelse(e02.dem.incum==1 & e02.rep.incum==0 & e02.party==2,1,ifelse(e02.dem.incum==0 & e02.rep.incum==1 & e02.party==1,1,ifelse(e02.dem.incum==1 & e02.rep.incum==0 & e02.party==1,0,ifelse(e02.dem.incum==0 & e02.rep.incum==1 & e02.party==2,0,ifelse(e02.party==3,0,NA)))))
e02.shares.inc.party <- ifelse(e02.dem.incum==1 & e02.rep.incum==0 & e02.party==1,1,ifelse(e02.dem.incum==0 & e02.rep.incum==1 & e02.party==2,1,ifelse(e02.dem.incum==1 & e02.rep.incum==0 & e02.party==2,0,ifelse(e02.dem.incum==0 & e02.rep.incum==1 & e02.party==1,0,ifelse(e02.party==3,0,NA)))))

e00.shares.chal.party <- ifelse(e00.dem.incum==1 & e00.rep.incum==0 & e00.party==2,1,ifelse(e00.dem.incum==0 & e00.rep.incum==1 & e00.party==1,1,ifelse(e00.dem.incum==1 & e00.rep.incum==0 & e00.party==1,0,ifelse(e00.dem.incum==0 & e00.rep.incum==1 & e00.party==2,0,ifelse(e00.party==3,0,NA)))))
e00.shares.inc.party <- ifelse(e00.dem.incum==1 & e00.rep.incum==0 & e00.party==1,1,ifelse(e00.dem.incum==0 & e00.rep.incum==1 & e00.party==2,1,ifelse(e00.dem.incum==1 & e00.rep.incum==0 & e00.party==2,0,ifelse(e00.dem.incum==0 & e00.rep.incum==1 & e00.party==1,0,ifelse(e00.party==3,0,NA)))))

e98.shares.chal.party <- ifelse(e98.dem.incum==1 & e98.rep.incum==0 & e98.party==2,1,ifelse(e98.dem.incum==0 & e98.rep.incum==1 & e98.party==1,1,ifelse(e98.dem.incum==1 & e98.rep.incum==0 & e98.party==1,0,ifelse(e98.dem.incum==0 & e98.rep.incum==1 & e98.party==2,0,ifelse(e98.party==3,0,NA)))))
e98.shares.inc.party <- ifelse(e98.dem.incum==1 & e98.rep.incum==0 & e98.party==1,1,ifelse(e98.dem.incum==0 & e98.rep.incum==1 & e98.party==2,1,ifelse(e98.dem.incum==1 & e98.rep.incum==0 & e98.party==2,0,ifelse(e98.dem.incum==0 & e98.rep.incum==1 & e98.party==1,0,ifelse(e98.party==3,0,NA)))))

e96.shares.chal.party <- ifelse(e96.dem.incum==1 & e96.rep.incum==0 & e96.party==2,1,ifelse(e96.dem.incum==0 & e96.rep.incum==1 & e96.party==1,1,ifelse(e96.dem.incum==1 & e96.rep.incum==0 & e96.party==1,0,ifelse(e96.dem.incum==0 & e96.rep.incum==1 & e96.party==2,0,ifelse(e96.party==3,0,NA)))))
e96.shares.inc.party <- ifelse(e96.dem.incum==1 & e96.rep.incum==0 & e96.party==1,1,ifelse(e96.dem.incum==0 & e96.rep.incum==1 & e96.party==2,1,ifelse(e96.dem.incum==1 & e96.rep.incum==0 & e96.party==2,0,ifelse(e96.dem.incum==0 & e96.rep.incum==1 & e96.party==1,0,ifelse(e96.party==3,0,NA)))))

e94.shares.chal.party <- ifelse(e94.dem.incum==1 & e94.rep.incum==0 & e94.party==2,1,ifelse(e94.dem.incum==0 & e94.rep.incum==1 & e94.party==1,1,ifelse(e94.dem.incum==1 & e94.rep.incum==0 & e94.party==1,0,ifelse(e94.dem.incum==0 & e94.rep.incum==1 & e94.party==2,0,ifelse(e94.party==3,0,NA)))))
e94.shares.inc.party <- ifelse(e94.dem.incum==1 & e94.rep.incum==0 & e94.party==1,1,ifelse(e94.dem.incum==0 & e94.rep.incum==1 & e94.party==2,1,ifelse(e94.dem.incum==1 & e94.rep.incum==0 & e94.party==2,0,ifelse(e94.dem.incum==0 & e94.rep.incum==1 & e94.party==1,0,ifelse(e94.party==3,0,NA)))))

e92.shares.chal.party <- ifelse(e92.dem.incum==1 & e92.rep.incum==0 & e92.party==2,1,ifelse(e92.dem.incum==0 & e92.rep.incum==1 & e92.party==1,1,ifelse(e92.dem.incum==1 & e92.rep.incum==0 & e92.party==1,0,ifelse(e92.dem.incum==0 & e92.rep.incum==1 & e92.party==2,0,ifelse(e92.party==3,0,NA)))))
e92.shares.inc.party <- ifelse(e92.dem.incum==1 & e92.rep.incum==0 & e92.party==1,1,ifelse(e92.dem.incum==0 & e92.rep.incum==1 & e92.party==2,1,ifelse(e92.dem.incum==1 & e92.rep.incum==0 & e92.party==2,0,ifelse(e92.dem.incum==0 & e92.rep.incum==1 & e92.party==1,0,ifelse(e92.party==3,0,NA)))))

##################################
# MERGE COOK CATEGORIES
#       note: it does not matter whether cook category is looked up in the senate.dem
#               data table or the senate.rep data table (either can be used).
##################################

cook.cat <- ifelse(toupper(senate.dem$cook)=="TOSSUPDEM" | toupper(senate.dem$cook)=="TOSSUPREP",3,ifelse(toupper(senate.dem$cook)=="LEANDEM" | toupper(senate.dem$cook)=="LEANREP",2,ifelse(toupper(senate.dem$cook)=="LIKELYDEM" | toupper(senate.dem$cook)=="LIKELYREP",1,ifelse(toupper(senate.dem$cook)=="SOLIDDEM" | toupper(senate.dem$cook)=="SOLIDREP",0,NA))))

e06.cook <- cook.cat[match(e06.id,senate.dem.id)]
e02.cook <- cook.cat[match(e02.id,senate.dem.id)]
e00.cook <- cook.cat[match(e00.id,senate.dem.id)]
e98.cook <- cook.cat[match(e98.id,senate.dem.id)]
e96.cook <- cook.cat[match(e96.id,senate.dem.id)]
e94.cook <- cook.cat[match(e94.id,senate.dem.id)]
e92.cook <- cook.cat[match(e92.id,senate.dem.id)]

###################################
# construct independent and dependent variables for all years
###################################

y.chal.vote <- c(e92.vote.chal,e94.vote.chal,e96.vote.chal,e98.vote.chal,e00.vote.chal,e02.vote.chal,e06.vote.chal)
y.inc.vote <- ifelse(y.chal.vote==1,0,ifelse(y.chal.vote==0,1,NA))
x.face.chal <- c(e92.face.chal,e94.face.chal,e96.face.chal,e98.face.chal,e00.face.chal,e02.face.chal,e06.face.chal)
x.face.inc <- c(e92.face.inc,e94.face.inc,e96.face.inc,e98.face.inc,e00.face.inc,e02.face.inc,e06.face.inc)
x.cook <- c(e92.cook,e94.cook,e96.cook,e98.cook,e00.cook,e02.cook,e06.cook)
x.shares.inc.party <- c(e92.shares.inc.party,e94.shares.inc.party,e96.shares.inc.party,e98.shares.inc.party,e00.shares.inc.party,e02.shares.inc.party,e06.shares.inc.party)
x.shares.chal.party <- c(e92.shares.chal.party,e94.shares.chal.party,e96.shares.chal.party,e98.shares.chal.party,e00.shares.chal.party,e02.shares.chal.party,e06.shares.chal.party)
x.year <- c(rep(1992,length(e92.vote.chal)),rep(1994,length(e94.vote.chal)),rep(1996,length(e96.vote.chal)),rep(1998,length(e98.vote.chal)),rep(2000,length(e00.vote.chal)),rep(2002,length(e02.vote.chal)),rep(2006,length(e06.vote.chal)))
x.inc.pres.margin <- c(e92.inc.pres.margin,e94.inc.pres.margin,e96.inc.pres.margin,e98.inc.pres.margin,e00.inc.pres.margin,e02.inc.pres.margin,e06.inc.pres.margin)
x.chal.receipts <- c(e92.chal.receipts,e94.chal.receipts,e96.chal.receipts,e98.chal.receipts,e00.chal.receipts,e02.chal.receipts,e06.chal.receipts)
x.sen.statepop <- c(e92.sen.statepop,e94.sen.statepop,e96.sen.statepop,e98.sen.statepop,e00.sen.statepop,e02.sen.statepop,e06.sen.statepop)
x.chal.expend <- c(e92.chal.expend,e94.chal.expend,e96.chal.expend,e98.chal.expend,e00.chal.expend,e02.chal.expend,e06.chal.expend)
x.inc.yrsinoffice <- c(e92.inc.yrsinoffice,e94.inc.yrsinoffice,e96.inc.yrsinoffice,e98.inc.yrsinoffice,e00.inc.yrsinoffice,e02.inc.yrsinoffice,e06.inc.yrsinoffice)
x.inc.age <- c(e92.inc.age,e94.inc.age,e96.inc.age,e98.inc.age,e00.inc.age,e02.inc.age,e06.inc.age)

z.open.seat <- c(e92.open.seat,e94.open.seat,e96.open.seat,e98.open.seat,e00.open.seat,e02.open.seat,e06.open.seat)
z.id <- c(e92.id,e94.id,e96.id,e98.id,e00.id,e02.id,e06.id)


x.cook.tossup <- ifelse(x.cook==3,1,0)
x.cook.lean <- ifelse(x.cook==2,1,0)
x.cook.likely <- ifelse(x.cook==1,1,0)
x.cook.safe <- ifelse(x.cook==0,1,0)

x.chal.receipts.log <- log(x.chal.receipts)
x.chal.expend.log <- log(x.chal.expend)

state.id <- z.id

x.partisanship <- ifelse(x.shares.inc.party==1,1,ifelse(x.shares.chal.party==1,-1,ifelse(x.shares.inc.party==0 & x.shares.chal.party==0,0,NA)))

# Prepare the data frame for the probit analysis.
exit.senate <- data.frame(cbind(y.chal.vote,y.inc.vote,x.face.chal,x.face.inc,x.cook,x.partisanship,x.shares.inc.party,x.shares.chal.party,x.year,x.cook.tossup,x.cook.lean,x.cook.likely,x.cook.safe,x.inc.pres.margin,x.chal.receipts,x.sen.statepop,x.chal.receipts.log,x.chal.expend,x.chal.expend.log,x.inc.yrsinoffice,x.inc.age),stringsAsFactors=F)
names(exit.senate) <- c("chal.vote","inc.vote","face.chal","face.inc","cook","partisanship","shares.inc.party","shares.chal.party","year","cook.tossup","cook.lean","cook.likely","cook.safe","inc.pres.margin","chal.receipts","statepop","chal.receipts.log","chal.expend","chal.expend.log","inc.yrsinoffice","inc.age")

exit.senate = cbind(exit.senate,state.id)

##REMOVE NA'S FROM SENATE DATA
exit.senate = exit.senate[is.na(exit.senate$state.id)==F&is.na(exit.senate$inc.vote)==F&is.na(exit.senate$cook)==F&is.na(exit.senate$shares.chal.party)==F&is.na(exit.senate$shares.inc.party)==F&is.na(exit.senate$face.chal)==F&is.na(exit.senate$face.inc)==F,]

###############################################################################################
# HOUSE: construct variables
###############################################################################################

dem.pres.margin <- aeh.house$dem_pres04/(aeh.house$dem_pres04+aeh.house$rep_pres04)
rep.pres.margin <- aeh.house$rep_pres04/(aeh.house$dem_pres04+aeh.house$rep_pres04)
inc.pres.margin <- ifelse(aeh.house$dem.incum==1,dem.pres.margin,rep.pres.margin)
cook.cat <- ifelse(toupper(aeh.house$cook)=="TOSSUPDEM" | toupper(aeh.house$cook)=="TOSSUPREP",3,ifelse(toupper(aeh.house$cook)=="LEANDEM" | toupper(aeh.house$cook)=="LEANREP",2,ifelse(toupper(aeh.house$cook)=="LIKELYDEM" | toupper(aeh.house$cook)=="LIKELYREP",1,ifelse(toupper(aeh.house$cook)=="SAFE",0,NA))))

##########################################################################################
# From AEH survey, face ratings of incumbent and challenger
##########################################################################################

face.inc <- ifelse(aeh.house$dem.incum==1,aeh.house$dem_comp,aeh.house$rep_comp)
face.chal <- ifelse(aeh.house$dem.incum==0,aeh.house$dem_comp,aeh.house$rep_comp)
vote.inc <- ifelse(aeh.house$dem.incum==1,aeh.house$dem_pctvote-aeh.house$rep_pctvote,aeh.house$rep_pctvote-aeh.house$dem_pctvote)
vote.chal <- ifelse(aeh.house$dem.incum==0,aeh.house$dem_pctvote-aeh.house$rep_pctvote,aeh.house$rep_pctvote-aeh.house$dem_pctvote)

##########################################################################################
# construct exit poll variables
##########################################################################################

house.chal.receipts <- ifelse(aeh.house$dem.incum==1 & aeh.house$rep.incum==0,aeh.house$rep_receipts,ifelse(aeh.house$dem.incum==0 & aeh.house$rep.incum==1,aeh.house$dem_receipts,NA))
house.chal.receipts <- ifelse(is.na(house.chal.receipts)==1 | house.chal.receipts<1,.5,house.chal.receipts)
house.chal.expend <- ifelse(aeh.house$dem.incum==1 & aeh.house$rep.incum==0,aeh.house$rep_expend,ifelse(aeh.house$dem.incum==0 & aeh.house$rep.incum==1,aeh.house$dem_expend,NA))
house.chal.expend <- ifelse(house.chal.expend==99999,NA,house.chal.expend) # NES context missing code is 99999
house.chal.expend <- ifelse(is.na(house.chal.expend)==1 | house.chal.expend<1,.5,house.chal.expend)

house.chal.receipts <- house.chal.receipts/1000 # NES has in 1,000 so this takes units to 100,000
house.chal.expend <- house.chal.expend/1000 # NES has in 1,000 so this takes units to 100,000

# pid (3 point measure)
exit.pid <- ifelse(as.numeric(exit04$PARTYID)==1,-1,ifelse(as.numeric(exit04$PARTYID)==2,1,0))

exit04.collgrad <- ifelse(exit04$COLLEDUC=="College graduate",1,
                    ifelse(exit04$COLLEDUC=="No degree",0,NA))

exit04.educ <- ifelse(is.na(exit04$EDUC),2.5,
                ifelse(exit04$EDUC=="Did not complete high school",1,
                ifelse(exit04$EDUC=="High school graduate",2,
                ifelse(exit04$EDUC=="Some college/associate degree",3,
                ifelse(exit04$EDUC=="College graduate",4,
                ifelse(exit04$EDUC=="Postgraduate study",5,2.5))))))
exit04.educMissing <- ifelse(is.na(exit04$EDUC),1,0)

exit04.female <- ifelse(is.na(exit04$SEX),.5,
                    ifelse(exit04$SEX=="Female",1,0))

hold.state <- exit.statecodes[match(exit04$STANUM,exit.statecodes[,3]),1]
district.exit04 <- paste(hold.state,exit04$CDNUM,sep="")

exit04.cook <- cook.cat[match(district.exit04,aeh.house$stcd)]

exit04.dem.incum <- aeh.house$dem.incum[match(district.exit04,aeh.house$stcd)]
exit04.rep.incum <- aeh.house$rep.incum[match(district.exit04,aeh.house$stcd)]
exit04.face.chal <- face.chal[match(district.exit04,aeh.house$stcd)]
exit04.face.inc <- face.inc[match(district.exit04,aeh.house$stcd)]
exit04.inc.pres.margin <- inc.pres.margin[match(district.exit04,aeh.house$stcd)]
exit04.chal.receipts <- house.chal.receipts[match(district.exit04,aeh.house$stcd)]
exit04.chal.expend <- house.chal.expend[match(district.exit04,aeh.house$stcd)]
exit04.inc.yrsinoffice <- 2004-aeh.house$IncFirstYrInOffice[match(district.exit04,aeh.house$stcd)]
exit04.inc.age <- 2004-aeh.house$IncBirthYr[match(district.exit04,aeh.house$stcd)]

voted.dem <- ifelse(as.numeric(exit04$HOU04)==2,1,ifelse(as.numeric(exit04$HOU04)==3,0,NA))
voted.rep <- ifelse(as.numeric(exit04$HOU04)==3,1,ifelse(as.numeric(exit04$HOU04)==2,0,NA))
exit04.voted.for.chal <- ifelse(exit04.dem.incum==0,voted.dem,voted.rep)
exit04.voted.for.inc <- ifelse(exit04.voted.for.chal==0,1,ifelse(exit04.voted.for.chal==1,0,NA))

exit04.shares.chal.party <- ifelse(exit04.dem.incum==0 & exit.pid==-1,1,
                                ifelse(exit04.dem.incum==1 & exit.pid==1,1,
                                ifelse(exit04.dem.incum==0 & exit.pid==1,0,
                                ifelse(exit04.dem.incum==1 & exit.pid==-1,0,
                                ifelse(exit.pid==0,0,NA)))))

exit04.shares.inc.party <- ifelse(exit04.dem.incum==1 & exit.pid==-1,1,
                                ifelse(exit04.dem.incum==0 & exit.pid==1,1,
                                ifelse(exit04.dem.incum==1 & exit.pid==1,0,
                                ifelse(exit04.dem.incum==0 & exit.pid==-1,0,
                                ifelse(exit.pid==0,0,NA)))))

# construct indicator variables for each cook category:

exit04.cook.tossup <- ifelse(exit04.cook==3,1,0)
exit04.cook.lean <- ifelse(exit04.cook==2,1,0)
exit04.cook.likely <- ifelse(exit04.cook==1,1,0)
exit04.cook.safe <- ifelse(exit04.cook==0,1,0)

exit04.chal.receipts.log <- log(exit04.chal.receipts)
exit04.chal.expend.log <- log(exit04.chal.expend)

exit.04.partisanship <- ifelse(exit04.shares.inc.party==1,1,ifelse(exit04.shares.chal.party==1,-1,ifelse(exit04.shares.inc.party==0 & exit04.shares.chal.party==0,0,NA)))

# Prepare the House data frame for the probit analysis
exit.house <- data.frame(exit04.cook,exit04.voted.for.inc,exit04.cook.lean,exit04.cook.likely,
                        exit04.cook.safe,exit.04.partisanship,exit04.shares.chal.party,
                        exit04.shares.inc.party,exit04.face.chal,exit04.face.inc,
                        exit04.inc.pres.margin,exit04.chal.receipts,exit04.chal.receipts.log,
                        exit04.chal.expend,exit04.chal.expend.log,
                        exit04.collgrad,exit04.educ,exit04.educMissing,exit04.female,
                        district.exit04,exit04.inc.yrsinoffice,exit04.inc.age)
names(exit.house) <- c("cook","inc.vote","cook.lean","cook.likely","cook.safe","partisanship",
                    "shares.chal.party","shares.inc.party","face.chal","face.inc",
                    "inc.pres.margin","chal.receipts","chal.receipts.log",
                    "chal.expend","chal.expend.log",
                    "college.grad","education","education.missing","female","district","inc.yrsinoffice","inc.age")

##REMOVE NA'S FROM HOUSE DATA
exit.house = exit.house[is.na(exit.house$district)==F&is.na(exit.house$inc.vote)==F&is.na(exit.house$cook)==F&is.na(exit.house$shares.chal.party)==F&is.na(exit.house$shares.inc.party)==F&is.na(exit.house$face.chal)==F&is.na(exit.house$face.inc)==F,]

###########################################
# Senate: statepop and chal vote cor
###########################################

sen.chal.votes <- aeh.senate$votes[aeh.senate$incumbent==0]
sen.chal.contests <- aeh.senate$contest[aeh.senate$incumbent==0]
sen.chal.votes <- sen.chal.votes[match(aeh.senate$contest,sen.chal.contests)] # entered in both incumbent's and challenger's rows
sen.inc.votes <- aeh.senate$votes[aeh.senate$incumbent==1]
sen.inc.contests <- aeh.senate$contest[aeh.senate$incumbent==1]
sen.inc.votes <- sen.inc.votes[match(aeh.senate$contest,sen.inc.contests)] # entered in both incumbent's and challenger's rows
chal.vote.share <- sen.chal.votes/(sen.chal.votes+sen.inc.votes)

exit.house = exit.house[is.na(exit.house$district)==F&is.na(exit.house$inc.vote)==F&is.na(exit.house$cook)==F&is.na(exit.house$shares.chal.party)==F&is.na(exit.house$shares.inc.party)==F&is.na(exit.house$face.chal)==F&is.na(exit.house$face.inc)==F,]


################################################
#COMBINED DATA FOR POOLED ANALAYSIS
################################################
exit.house$house = 1
exit.house$senate = 0
exit.senate$house = 0
exit.senate$senate = 1

exit.house$state.id[1:nrow(exit.house)] = substr(as.character(exit.house$district[1:nrow(exit.house)]),1,2)[1:nrow(exit.house)]
exit.house$state.id = paste("2004",exit.house$state.id,sep="")
    
exit.both <- rbind(exit.house[,c('inc.vote','cook','partisanship','shares.chal.party','shares.inc.party','face.chal','face.inc','chal.expend','chal.expend.log','inc.yrsinoffice','inc.age', 'house','senate','state.id')],
        exit.senate[,c('inc.vote','cook','partisanship','shares.chal.party','shares.inc.party','face.chal','face.inc','chal.expend','chal.expend.log', 'inc.yrsinoffice','inc.age','house','senate','state.id')])

#
# Save out data frames to R data file.
#
save(exit.house,exit.senate,exit.both,senate.dem,
    senate.rep,aeh.senate,aeh.house,file='exit.poll.Rdata')
